<template>
  <div className="app-container">
    <div className="table-container">
      <base-list
        ref="table"
        url="/user_read_plan"
        :options="options"
        :columns="columns"
        :format="format"
        :rules="rules"
        permission="ADMIN"
        @reloadOptions="handleGetOptions"
      >
        <template slot="header_actions" slot-scope="scope">
          <el-button v-waves type="warning" class="filter-button" icon="el-icon-refresh" @click="refreshActualReadNum()">刷新实际阅读量</el-button>
        </template>
      </base-list>
    </div>
  </div>
</template>

<script>
import {getBaseList, getBaseOptions, requestBase} from '@/api/common'
import BaseList from '@/components/BaseList/index.vue'

export default {
  name: 'UserReadPlan',
  components: {BaseList},
  data() {
    return {
      columns: [
        {show: true, edit: true, width: 'auto',minWidth: '300', key: 'user', label: '用户', tooltip: '用户', type: 'REMOTE_SELECT', remote: {url: '/user_info', label: 'nickName',multiple: false}, queryable: true},
        {show: true, edit: true, width: 'auto',minWidth: '200', key: 'planReadNum', label: '计划阅读数量', tooltip: '计划阅读数量', type: 'NUMBER', sortable: true, queryable: true},
        {show: true, edit: true, width: 'auto',minWidth: '200', key: 'actualReadNum', label: '当前阅读数量', tooltip: '当前阅读数量', type: 'NUMBER', sortable: true, queryable: true},
        {show: true, edit: true, width: 'auto',minWidth: '200', key: 'year', label: '年份', tooltip: '年份', type: 'NUMBER', sortable: true, queryable: true},
        {show: true, edit: true, width: 'auto',minWidth: '200', key: 'yearDays', label: '当年天数', tooltip: '当年天数', type: 'NUMBER'},
        {show: false, edit: false, width: 'auto',minWidth: '200', key: 'createTime', label: '创建时间', tooltip: '创建时间', type: 'DATE_TIME'},
        {show: true, edit: false, width: 'auto',minWidth: '200', key: 'updateTime', label: '更新时间', tooltip: '更新时间', type: 'DATE_TIME', queryable: true},
        {show: false, edit: false, width: 'auto',minWidth: '200', key: 'createUser', label: '创建人', tooltip: '创建人', type: 'WORD'},
        {show: false, edit: false, width: 'auto',minWidth: '200', key: 'updateUser', label: '修改人', tooltip: '修改人', type: 'WORD'},
      ],
      format: {
        user: (data, index) => {
          return data.user ? data.user.nickName + '('+data.user.id+')' : ''
        },
        createUser: (data, index) => {
          return data.createUser ? data.createUser.nickName + '('+data.createUser.id+')' : ''
        },
        updateUser: (data, index) => {
          return data.updateUser ? data.updateUser.nickName + '('+data.updateUser.id+')' : ''
        }
      },
      rules: {
        add: {
          userId: [{required: true, message: '用户ID必填!', trigger: 'blur'}],
          planReadNum: [{required: true, message: '计划阅读数量必填!', trigger: 'blur'}],
          actualReadNum: [{required: true, message: '当前阅读数量必填!', trigger: 'blur'}],
          year: [{required: true, message: '年份必填!', trigger: 'blur'}],
          yearDays: [{required: true, message: '当年天数必填!', trigger: 'blur'}],

        },
        edit: {
          id: [{required: true, message: '请选择数据!', trigger: 'change'}],
          userId: [{required: true, message: '用户ID必填!', trigger: 'blur'}],
          planReadNum: [{required: true, message: '计划阅读数量必填!', trigger: 'blur'}],
          actualReadNum: [{required: true, message: '当前阅读数量必填!', trigger: 'blur'}],
          year: [{required: true, message: '年份必填!', trigger: 'blur'}],
          yearDays: [{required: true, message: '当年天数必填!', trigger: 'blur'}],

        }
      },
      options: {
      }
    }
  },

  created() {
    this.handleGetOptions()
  },
  mounted() {
  },
  methods: {
    refreshActualReadNum() {
      requestBase({
        url: '/user_read_plan/sum_actual_read_num',
        method: 'POST'
      }).then(res => {
        this.$notify.success('刷新成功!')
      })
    },
    handleGetOptions() {
      const options = JSON.parse(localStorage.getItem('allOptions'))
      if (options) {
        this.options = Object.assign(this.options, options)
      }
    }
  }
}
</script>


